/*
 * $Id: ArticleCategory.java 46 2012-02-17 12:50:01Z rook.alex@gmail.com $
 */
package ua.org.energy.vkek.site.model;

import com.google.appengine.api.datastore.Key;
import java.io.Serializable;
import java.util.List;
import java.util.Stack;
import javax.persistence.*;
import ua.org.energy.vkek.site.server.DataIOException;

@NamedQueries({
    @NamedQuery(name = "ArticleCategory.findAll",
    query = "SELECT c from ArticleCategory c"),
    @NamedQuery(name = "ArticleCategory.findByName",
    query = "SELECT obj from ArticleCategory obj WHERE obj.categoryName=:catName"),
    @NamedQuery(name = "ArticleCategory.findCount",
    query = "SELECT count(c) from ArticleCategory c"),
    @NamedQuery(name = "ArticleCategory.findTagsByCategory",
    query = "SELECT obj from ArticleTag obj WHERE obj.category=:category")
})
@Entity
public class ArticleCategory extends AbstractEntity implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Key ident;
//    @Column(unique = true) //java.lang.UnsupportedOperationException: No support for uniqueness constraints :-(
    private String categoryName;
    @OneToMany(mappedBy = "category")
    private List<ArticleTag> articleTags;

    /**
     * @return the categoryName
     */
    public String getCategoryName() {
        return categoryName;
    }

    /**
     * @param categoryName the categoryName to set
     */
    public void setCategoryName(String categoryName) throws DataIOException {
        this.categoryName = setFieldWise(this.categoryName, categoryName, true, new String[]{"", "undefined"});
    }

    /**
     * @return the ident
     */
    public Key getIdent() {
        return ident;
    }

    /**
     * @param ident the ident to set
     */
    public void setIdent(Key ident) {
        this.ident = ident;
    }

    /**
     * @return the articleTags
     */
    public List<ArticleTag> getArticleTags() {
        return articleTags;
    }

    /**
     * @param articleTags the articleTags to set
     */
    public void setArticleTags(List<ArticleTag> articleTags) {
        this.setArticleTags(articleTags);
    }

    @Override
    public boolean equals(Object obj) {
        if (!(obj instanceof ArticleCategory)) {
            return false;
        }
        return (this == obj);
    }

    @Override
    public int hashCode() {
        int hash = 3;
        hash = 53 * hash + (this.ident != null ? this.ident.hashCode() : 0);
        hash = 53 * hash + (this.categoryName != null ? this.categoryName.hashCode() : 0);
        return hash;
    }

    @Override
    public ArticleCategory webSafeClone() {
        ArticleCategory buf = new ArticleCategory();
        try {
            buf.setIdent(this.getIdent());
            buf.setCategoryName(this.getCategoryName());
        } finally {
            return buf;
        }
    }

    @Override
    public Stack<EntityPathElement> getEntityPath() {
        Stack<EntityPathElement> result = new Stack<EntityPathElement>();
        EntityPathElement mypathEl=getPathElement();
        result.push(mypathEl);
        return result;
    }

    @Override
    public EntityPathElement getPathElement() {
        return new EntityPathElementImpl(categoryName, ident);
    }
    
    @Override
    public String toString(){
        return getClass().getName()+",Key:"+
                getIdent().toString()+",CategoryName:"+getCategoryName();
    }
}
